extends BaseStage

const Transiton := preload("res://Scenes/UI/TransitionScene.tscn")

@export var main_menu:String
@export var before_boss_dialogue_count:int
@export var push_speed:float = 1200

@onready var spike_43: Area2D = $BossRoomSpike/Spike43
@onready var spike_44: Area2D = $BossRoomSpike/Spike44
@onready var boss_room_spike: Node2D = $BossRoomSpike
@onready var fake_brick_2: StaticBody2D = $FakeBrick2
@onready var fake_brick: StaticBody2D = $FakeBrick
@onready var node_2d: Node2D = $Node2D
@onready var phase_2: Node2D = $phase2
@onready var area_2d: Area2D = $Area2D

func _ready() -> void:
	super._ready()
	Global.camera.top_level = false
	Global.main.sub_layer.visible = false
	Global.main.ui.visible = false
	area_2d.body_entered.connect(on_tmpe_entered.unbind(1))
	Global.current_stage_name = "stage4"

func show_sprite(index:int) -> void:
	var tween = create_tween()
	tween.tween_property(node_2d.get_child(index),"modulate:a",1,.3)
	await tween.finished

func _process(delta: float) -> void:
	if Global.player.global_position.distance_squared_to($FakeBg.global_position) < pow(640,2):
		$FakeBg.queue_free()
		set_process(false)

func queue_free_sprite() -> void:
	var tween = create_tween()
	tween.tween_property(node_2d,"modulate:a",0,.3)
	await tween.finished
	node_2d.queue_free()

func on_dialogue_end() -> void:
	super.on_dialogue_end()
	if current_count == 1:
		$ColorRect.queue_free()
		Global.main.sub_layer.visible = true
		Global.main.ui.visible = true
	elif current_count == before_boss_dialogue_count:
		current_npc.red_temp()
		Global.player.is_dead = false
		Global.camera.top_level = false
		Global.camera.offset = Vector2.ZERO
	elif current_count == 6:
		var trasition :Transition= Transiton.instantiate()
		trasition.next_scene_path = main_menu
		add_child(trasition)

func delete_dialogue_area(count: int) -> void:
	Global.main.sub_layer.visible = true
	Global.main.ui.visible = true
	super.delete_dialogue_area(count)
	current_npc.change_animate("cower_change_idle")
	current_npc.red_temp()
	$ColorRect.queue_free()
	if count >= 2:
		await get_tree().create_timer(.7).timeout
		Global.main.sub_layer.visible = true
		Global.main.ui.visible = true
		Global.camera.top_level = false
		Global.camera.offset = Vector2.ZERO
	


func _on_check_point_2_body_entered(body: Player) -> void:
	if Global.current_setting.get("MODE",2)==2:
		body.max_energy = 2
	else:
		body.max_energy = 3
	current_npc.change_emotion(Npc.Emotion.MANIA)
	body.current_energy = body.max_energy
	current_npc.start_index = 3
		


func _on_check_point_4_body_entered(body: Player) -> void:
	if Global.current_setting.get("MODE",2)==2:
		body.max_energy = 2
	else:
		body.max_energy = 3
	body.current_energy = body.max_energy
	current_npc.change_emotion(Npc.Emotion.DEPRESSED)
	current_npc.hurt_box.queue_free()
	current_npc.start_index = 12
	await get_tree().create_timer(.7).timeout
	Global.player.velocity = Vector2.RIGHT * push_speed
	
	
	
func on_tmpe_entered() -> void:
	fake_brick.visible = true
	fake_brick_2.visible = true
	fake_brick.process_mode = Node.PROCESS_MODE_INHERIT
	fake_brick_2.process_mode = Node.PROCESS_MODE_INHERIT

func _on_check_point_3_body_entered(body: Node2D) -> void:
	current_npc.change_emotion(Npc.Emotion.MANIA)
	current_npc.start_index = 6


func _on_black_player_half_hp() -> void:
	spike_43.visible = true
	spike_44.visible = true
	spike_43.monitoring = true
	spike_44.monitoring = true
	for i:Node2D in boss_room_spike.get_children():
		i.move_up()
	phase_2.visible = true
	for i:Area2D in phase_2.get_children():
		i.monitoring = true


func _on_buff_body_entered(body: Node2D) -> void:
	current_npc.hurt()
	if current_npc.current_hp == 0:
		for i:Node2D in boss_room_spike.get_children():
			i.move_down()


func _on_check_point_5_body_entered(body: Node2D) -> void:
	if Global.current_setting.get("MODE",2)==2:
		body.max_energy = 2
	else:
		body.max_energy = 3
	current_npc.change_emotion(Npc.Emotion.MANIA)
	current_npc.start_index = 12
